#include "piongraphics.h"

#include "etat.h"
#include "pion.h"
#include "joueur.h"
#include "plateaurisq.h"

#define PI 3.14159265358979323846
#define DEG2RAD(DEG) ((DEG)*((PI)/(180.0)))


PionGraphics::PionGraphics(Pion *pion) : QObject(), QGraphicsItem()
{
    this->pion = pion;

    setCursor(Qt::OpenHandCursor);
    setAcceptedMouseButtons(Qt::LeftButton);

    setFlag(ItemSendsGeometryChanges);
    setCacheMode(DeviceCoordinateCache);
    setZValue(2);

    connect(pion, SIGNAL(updateGraphics()), this, SLOT(updateGraphics()));
    connect(pion, SIGNAL(adjust(int,int)), this , SLOT(adjust(int,int)));
    show();
}


void PionGraphics::adjust(int pos, int max)
{
    if (!pion->getEtat())
        return;

    QPointF newPos = pion->getEtat()->pos();

    int radius = 15;
    double angleRad = DEG2RAD((360/max*pos)-45);
    double x = newPos.x() + (radius * qCos(angleRad));
    double y = newPos.y() + (radius * qSin(angleRad));

    newPos.setX(x);
    newPos.setY(y);

    setPos(newPos);
}

void PionGraphics::updateGraphics()
{
    update(boundingRect());
}

QRectF PionGraphics::boundingRect() const
{
    qreal adjust = 2;
    return QRectF(-10 - adjust, -10 - adjust,
                  23 + adjust, 23 + adjust);
}

QPainterPath PionGraphics::shape() const
{
    QPainterPath path;
    path.addEllipse(-5, -5, 10, 10);
    return path;
}

void PionGraphics::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *)
{
    Qt::GlobalColor color, colorDark;
    if ( pion->getJoueur() == NULL ){
        color = Qt::gray;
        colorDark = Qt::darkGray;
    }else{
        color = pion->getJoueur()->getCouleur();
        colorDark = pion->getJoueur()->getCouleurDark();
    }

    painter->setPen(Qt::NoPen);
    painter->setBrush(Qt::darkGray);
    painter->drawEllipse(-3, -3, 10, 10);

    QRadialGradient gradient(-3, -3, 10);
    if (option->state & QStyle::State_Sunken) {
        gradient.setCenter(3, 3);
        gradient.setFocalPoint(3, 3);
        gradient.setColorAt(1, QColor(color).light(120));
        gradient.setColorAt(0, QColor(colorDark).light(120));
    } else {
        gradient.setColorAt(0, color);
        gradient.setColorAt(1, colorDark);
    }
    painter->setBrush(gradient);
    painter->setPen(QPen(Qt::black, 0));
    painter->drawEllipse(-5, -5, 10, 10);
}

QVariant PionGraphics::itemChange(GraphicsItemChange change, const QVariant &value)
{
    switch (change) {
    case ItemPositionHasChanged:

        break;
    default:
        break;
    };

    return QGraphicsItem::itemChange(change, value);
}

void PionGraphics::mousePressEvent(QGraphicsSceneMouseEvent *event)
{

    Joueur *j = pion->getEtat()->getPlateau()->getMoi();

    // Verifie que l'état appartient au joueur
    if ( j == NULL || pion->getEtat()->getJoueur() != j )
        return;

    setCursor(Qt::ClosedHandCursor);

    QDrag *drag = new QDrag(event->widget());
    QMimeData *mime = new QMimeData;
    // Définie l'id de l'état comme valeur pour le drag&drop
    mime->setText(QString::number(pion->getEtat()->getId()));
    drag->setMimeData(mime);

    /* Censé faire le rendu du pion durant le drag&drop
    QPixmap pixmap(20, 20); // width et height, deux attributs de mon item
    QPainter pixPainter(&pixmap);
    QStyleOptionGraphicsItem *option = new QStyleOptionGraphicsItem();
    paint(&pixPainter, option, NULL);
    drag->setPixmap(pixmap);
    drag->setHotSpot(QPoint(pixmap.width()/2, pixmap.height()));
    */
    drag->exec();
    setCursor(Qt::OpenHandCursor);

}

void PionGraphics::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    setCursor(Qt::OpenHandCursor);

    update();
    QGraphicsItem::mouseReleaseEvent(event);
}

